package com.waidi.clothespad.adapter;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.waidi.clothespad.R;
import com.waidi.clothespad.common.Config;
import com.waidi.clothespad.common.FaultInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by ll on 2018/7/18.
 */

public class DuoXuanAdapter extends BaseAdapter {

    private List<String> beans;
    private Context mContext;
    public int now=0;//上一次选中的pos
    private boolean isFlag=false;
    private String TAG ="DuoXuanAdapter:";
    private int Selectednum = 0;

    public ArrayList<FaultInfo> faultInfoArrayList = new ArrayList<>();
    private int count = 21;

    public DuoXuanAdapter(Context mContext,List<String> beans) {
        // TODO Auto-generated constructor stub
        this.mContext = mContext;
        this.beans = beans;

        if(loadFaultInfo()==false)
        {
            for (int i = 0; i < count; i++)
            {
              FaultInfo tmp = new FaultInfo();
              tmp.fault_name = "掉线、漏针、拷边" + i;
              tmp.fault_code = Integer.toString(i);
              tmp.clickedList = 0;
              faultInfoArrayList.add(tmp);
            }
        }
    }


    public void setIsFlag(boolean flag){//这个如果是false那就没有默认选择的了。
        this.isFlag=flag;
    }


    public int getSelectNum()
    {
        return Selectednum;
    }

    public void clear()
    {
        Selectednum = 0;
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return faultInfoArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public boolean loadFaultInfo()
    {

        File faultinfotxt = new File(Config.faultinfotxt);


        if(faultinfotxt.exists())
        {
            try
            {
                FileInputStream in = new FileInputStream(faultinfotxt);
                int len = 0;
                byte[] buf = new byte[1024];
                String  strbuf;
                String  strjson="";
                while((len=in.read(buf))!=-1)
                {
                    strbuf = new String(buf,0,len);
                    strjson = strjson + strbuf;
                }
                in.close();
                if(strjson==null)
                    return false;

                JSONObject jsonObj = new JSONObject(strjson);
                if(jsonObj==null)
                    return false;
                if(jsonObj.has("data")==true)
                {
                    JSONArray qcArray = jsonObj.getJSONArray("data");
                    if (qcArray == null)
                        return false;

                    for (int i = 0; i < qcArray.length(); i++) {
                        JSONObject s = (JSONObject) qcArray.get(i);
                        FaultInfo tmp = new FaultInfo();
                        tmp.fault_name =s.getString("name");
                        tmp.fault_code = s.getString("type");
                        tmp.category_name = s.getString("categoryName");
                        tmp.clickedList = 0;
                        faultInfoArrayList.add(tmp);
                    }
                    return true;
                }
                else  return false;


            }
            catch (JSONException je)
            {
                je.printStackTrace();
                return false;
            }
            catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        else {
            return  false;
        }

    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        // TODO Auto-generated method stub
        try {


        final ViewHolder holder;
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.fault_item, null);
            holder = new ViewHolder();
            holder.tv_work = (TextView) convertView.findViewById(R.id.tv_fault);
            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.tv_work.setBackgroundColor(Color.TRANSPARENT);



/*      if(isFlag)
        {
           // holder.tv_work.setBackgroundResource(R.drawable.shengao_selected);
            holder.tv_work.setBackgroundColor(Color.rgb(222,233,12));
            isFlag=false;
        }*/

        //holder.tv_work.setTag("work"+position);
        //holder.tv_work.setText((beans.get(position)));
        holder.tv_work.setText(faultInfoArrayList.get(position).fault_name);

        holder.tv_work.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(faultInfoArrayList.get(position).clickedList==0)
                {
/*                  TextView tv=(TextView)parent.findViewWithTag("work"+2);
                    if(tv!=null)
                    {
                       // tv.setBackgroundResource(R.drawable.shengao_normal);
                        tv.setBackgroundColor(Color.rgb(222,233,12));
                    }*/
                  //  holder.tv_work.setBackgroundResource(R.drawable.shengao_selected);
                   // holder.tv_work.setBackgroundColor(Color.TRANSPARENT);
                   // holder.tv_work.setBackgroundColor(Color.rgb(222,233,12));

                    //now=position;
                    FaultInfo faultInfo = faultInfoArrayList.get(position);
                    faultInfo.clickedList = 1;
                    faultInfoArrayList.set(position,faultInfo);
                    holder.tv_work.setBackgroundColor(Color.rgb(222,233,12));
                    Selectednum++;
                }
                else
                {
                    FaultInfo faultInfo = faultInfoArrayList.get(position);
                    faultInfo.clickedList = 0;
                    faultInfoArrayList.set(position,faultInfo);
                    holder.tv_work.setBackgroundColor(Color.TRANSPARENT);
                    if(Selectednum>0)
                    Selectednum--;

                }
                Log.i(TAG,Integer.toString(position)+"#"+faultInfoArrayList.get(position).clickedList);

            }
        });
        Log.i(TAG,"*"+Integer.toString(position)+"#"+faultInfoArrayList.get(position).clickedList);
        //for(int i = 0;i<faultInfoArrayList.size();i++)
        {
            if(faultInfoArrayList.get(position).clickedList==1)
            {
                holder.tv_work.setBackgroundColor(Color.rgb(222,233,12));
               // Log.i(TAG,"yellow");
            }
            else
            {
                holder.tv_work.setBackgroundColor(Color.TRANSPARENT);
                //Log.i(TAG,"white");
            }
        }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }

        return convertView;
    }
    class ViewHolder {
        public 	TextView tv_work;
    }

}